﻿using CoreShop.Library;
using CoreShop.Services;
using Sunny.UI;
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIGoodsTypeLayout : UIUserControl
    {
        public UIGoodsTypeLayout()
        {
            InitializeComponent();
        }

        //public event EventHandler SetFoodListPre;
        //public event EventHandler SetFoodListNext;
        public event EventHandler SetBindFoodList; 
        //public event EventHandler SetFoodTypeAll;

        private int mParentRowCount = 0;//大类记录数
        private int mParentPageSize = 0;//大类页大小
        private int mParentPageIndex = 0;//大类页码
        private long mParentTypeId = 0;//大类ID
        private long mSelectTypeId = 0;//选择商品分类ID

        private int mChildRowCount = 0;//小类记录数
        private int mChildPageSize = 0;//小类页大小
        private int mChildPageIndex = 0;//小类页码

        private int mFoodRowCount = 0;//小类记录数
        private int mFoodPageSize = 0;//小类页大小
        private int mFoodPageIndex = 1;//小类页码

        public int FoodRowCount
        {
            get { return mFoodRowCount; }
            set
            {
                mFoodRowCount = value;
                Invalidate();
            }
        }

        public void SetFoodPre()
        {
           if (btnFoodPre.Enabled) btnFoodPre_Click(btnFoodPre,null);
        }

        public void SetFoodNext()
        {
            if (btnFoodNext.Enabled) btnFoodNext_Click(btnFoodNext, null);
        }

        public void SetFoodPageSize(int _FoodPageSize)
        {
            mFoodPageSize = _FoodPageSize;
        }

        public int GetFoodPageSize
        {
            get { return mFoodPageSize; }
        }

        public int FoodPageIndex
        {
            get { return mFoodPageIndex; }
            set
            {
                mFoodPageIndex = value;
                Invalidate();
            }
        }

        public bool GetFoodPreState
        {
            get { return btnFoodPre.Enabled; }
        }

        public bool GetFoodNextState
        {
            get { return btnFoodNext.Enabled; }
        }

        public bool SetCreateFoodType
        {
            get { return false; }
            set
            {
                if (value == true)
                {
                    LayoutForm();
                    btnAllType_Click(null, null);
                }
                Invalidate();
            }
        }

        private void CreateFoodTypeBtn(UIFlowLayoutPanel flyType, int index)
        {
            UIButton btn = new UIButton();
            btn.Name = flyType.Name + "_" + index.ToString();
            btn.Visible = true;
            btn.Enabled = false;
            btn.Tag = index;
            btn.Text = "商品分类_" + index.ToString();
            btn.BackColor = btnFoodType.BackColor;
            btn.Cursor = btnFoodType.Cursor;
            btn.FillColor = btnFoodType.FillColor;
            btn.FillColor2 = btnFoodType.FillColor2;
            btn.FillHoverColor = btnFoodType.FillHoverColor;
            btn.FillPressColor = btnFoodType.FillPressColor;
            btn.FillSelectedColor = btnFoodType.FillSelectedColor;
            btn.FillDisableColor = btnFoodType.FillDisableColor;
            btn.Font = btnFoodType.Font;
            btn.ForeHoverColor = btnFoodType.ForeHoverColor;
            btn.ForePressColor = btnFoodType.ForePressColor;
            btn.ForeSelectedColor = btnFoodType.ForeSelectedColor;
            btn.ForeDisableColor = btnFoodType.ForeDisableColor;
            btn.MinimumSize = btnFoodType.MinimumSize;
            btn.Radius = btnFoodType.Radius;
            btn.RectColor = btnFoodType.RectColor;
            btn.RectHoverColor = btnFoodType.RectHoverColor;
            btn.RectPressColor = btnFoodType.RectPressColor;
            btn.RectSelectedColor = btnFoodType.RectSelectedColor;
            btn.RectDisableColor = btnFoodType.RectDisableColor;
            btn.Size = btnFoodType.Size;
            btn.Style = btnFoodType.Style;
            btn.StyleCustomMode = btnFoodType.StyleCustomMode;
            btn.TipsFont = btnFoodType.TipsFont;
            btn.ForeColor = btnFoodType.ForeColor;
            btn.TipsColor = btnFoodType.TipsColor;
            btn.TipsForeColor = btnFoodType.TipsForeColor;
            btn.ShowTips = btnFoodType.ShowTips;
            btn.Margin = btnFoodType.Margin;    
            if (flyType.Name == flyParentType.Name)
                btn.Click += this.btnParentType_Click;
            else
                btn.Click += this.btnChildType_Click;
            flyType.Add(btn);
        }

        private void ClearParentTypeBtn()
        {
            for (int i = 1; i <= mParentPageSize + 1; i++)
            {
                string btnName0 = flyParentType.Name + "_" + i.ToString();
                UIButton btnType = (UIButton)GetControlInstance(this, btnName0);
                if (btnType != null)
                {
                    btnType.Enabled = false;
                    btnType.Selected = false;
                }
            }
        }

        private void ClearChildTypeBtn()
        {
            for (int i = 1; i <= mChildPageSize + 1; i++)
            {
                string btnName0 = flyChildType.Name + "_" + i.ToString();
                UIButton btnType = (UIButton)GetControlInstance(this, btnName0);
                if (btnType != null)
                {
                    btnType.Enabled = false;
                    btnType.Selected = false;
                }
            }
        }

        /// <summary>
        /// 根据指定容器和控件名字，获得控件
        /// </summary>
        /// <param name="obj">容器</param>
        /// <param name="strControlName">控件名字</param>
        /// <returns>控件</returns>
        private object GetControlInstance(object obj, string strControlName)
        {
            IEnumerator Controls = null;//所有控件
            Control c = null;//当前控件
            Object cResult = null;//查找结果
            if (obj.GetType() == this.GetType())//窗体
            {
                Controls = this.Controls.GetEnumerator();
            }
            else//控件
            {
                Controls = ((Control)obj).Controls.GetEnumerator();
            }
            while (Controls.MoveNext())//遍历操作
            {
                c = (Control)Controls.Current;//当前控件
                if (c.HasChildren)//当前控件是个容器
                {
                    cResult = GetControlInstance(c, strControlName);//递归查找
                    if (cResult == null)//当前容器中没有，跳出，继续查找
                        continue;
                    else//找到控件，返回
                        return cResult;
                }
                else if (c.Name == strControlName)//不是容器，同时找到控件，返回
                {
                    return c;
                }
            }
            return null;//控件不存在
        }

        private string GetFoodTypeButtonName(UIFlowLayoutPanel flyType, long foodTypeId)
        {
            return flyType.Name + "_" + foodTypeId.ToString();
        }

        private void GetTypeRowSize()
        {
            decimal mWidth = flyParentType.Width - 2;
            decimal btnCounts = mWidth / (btnFoodType.Width + 1);
            int mRowSize = CommFunc.ConvertToInt32(Math.Floor(btnCounts));
            if (AppConstVars.systemsetup.foodTypeBindType == (int)GlobalEnumVars.FoodTypeBindType.ShowFoodType)
            {
                mParentPageSize = 0;
                mChildPageSize = mRowSize * 2 - 1;
            }
            else
            {
                mParentPageSize = mRowSize - 1;
                mChildPageSize = mRowSize;
            }
        }

        /// <summary>
        /// 根据参数显示页按钮
        /// </summary>
        private void ShowPageButton()
        {

            if (AppConstVars.systemsetup.foodTypeBindType == (int)GlobalEnumVars.FoodTypeBindType.ShowFoodType)
            {                
                //只显示商品分类，大类不显示，小类的分布置顶，显示商品分类按钮
                btnParentTypePre.Visible = false;
                btnParentTypeNext.Visible = false;
                btnChildTypePre.Location = new Point(2, 3);
                btnChildTypeNext.Location = new Point(74, 3);
                btnFoodPre.Location = new Point(2, 62);
                btnFoodNext.Location = new Point(74, 62);
                btnFoodPre.Visible = true;
                btnFoodNext.Visible = true;
            }
         }

        private void LayoutForm()
        {
            GetTypeRowSize();
            ShowPageButton();
            CreateFoodType();
        }

        private void CreateFoodType()
        {
            if (AppConstVars.systemsetup.foodTypeBindType == (int)GlobalEnumVars.FoodTypeBindType.ShowFoodType)
            {
                flyParentType.Height = 0;
                flyParentType.Visible = false;
                btnAllType.Margin = new System.Windows.Forms.Padding(1, 2, 1, 3);
                btnFoodType.Margin = btnAllType.Margin;
                flyChildType.Clear();
                flyChildType.Add(btnAllType);
                flyChildType.Add(btnFoodType);
                for (int i = 1; i <= mChildPageSize; i++)
                {
                    CreateFoodTypeBtn(flyChildType, i);
                }                
            }
            else
            {
                btnAllType.Margin = new System.Windows.Forms.Padding(1, 1, 1, 0);
                btnFoodType.Margin = btnAllType.Margin;
                flyParentType.Height = pnl_center_top_foodtype.Height / 2;
                flyParentType.Clear();
                flyParentType.Add(btnAllType);
                flyParentType.Add(btnFoodType);
                for (int i = 1; i <= mParentPageSize; i++)
                {
                    CreateFoodTypeBtn(flyParentType, i);
                }
                for (int j = 1; j <= mChildPageSize + 1; j++)
                {
                    CreateFoodTypeBtn(flyChildType, j);
                }
            }
        }

        private void SetParentPageState()
        {
            if (mParentRowCount <= mParentPageSize)
            {
                btnParentTypePre.Enabled = false;
                btnParentTypeNext.Enabled = false;
            }
            else if (mParentPageIndex == 1)
            {
                btnParentTypePre.Enabled = false;
                btnParentTypeNext.Enabled = true;
            }
            else if (mParentPageIndex * mParentPageSize >= mParentRowCount)
            {
                btnParentTypePre.Enabled = true;
                btnParentTypeNext.Enabled = false;
            }
            else
            {
                btnParentTypePre.Enabled = true;
                btnParentTypeNext.Enabled = true;
            }
        }

        private void SetChildPageState()
        {            
            if (mChildRowCount <= mChildPageSize)
            {
                btnChildTypePre.Enabled = false;
                btnChildTypeNext.Enabled = false;
            }
            else if (mChildPageIndex == 1)
            {
                btnChildTypePre.Enabled = false;
                btnChildTypeNext.Enabled = true;
            }
            else if (mChildPageIndex * mChildPageSize >= mChildRowCount)
            {
                btnChildTypePre.Enabled = true;
                btnChildTypeNext.Enabled = false;
            }
            else
            {
                btnChildTypePre.Enabled = true;
                btnChildTypeNext.Enabled = true;
            }                      
        }

        public void SetFoodPageState()
        {
            if (mFoodRowCount <= mFoodPageSize)
            {
                btnFoodPre.Enabled = false;
                btnFoodNext.Enabled = false;
            }
            else if (mFoodPageIndex == 1)
            {
                btnFoodPre.Enabled = false;
                btnFoodNext.Enabled = true;
            }
            else if (mFoodPageIndex * mFoodPageSize >= mFoodRowCount)
            {
                btnFoodPre.Enabled = true;
                btnFoodNext.Enabled = false;
            }
            else
            {
                btnFoodPre.Enabled = true;
                btnFoodNext.Enabled = true;
            }      
        }

        private void BindParentType()
        {
            ClearParentTypeBtn();
            if (AppConstVars.systemsetup.foodTypeBindType == (int)GlobalEnumVars.FoodTypeBindType.ShowFoodType) return;
            int btnIndex = 1;
            if (mParentPageIndex < 1) mParentPageIndex = 1;
            int foodTypeBindType = AppConstVars.systemsetup.foodTypeBindType;
            var parentTypeList = new corecmsfoodcategoryServices().QueryParentAsync(ref mParentRowCount, foodTypeBindType, mParentPageIndex, mParentPageSize);
            foreach (var item in parentTypeList)
            {
                string btnName = GetFoodTypeButtonName(flyParentType,btnIndex);
                try
                {
                    UIButton btnType = (UIButton)GetControlInstance(this, btnName);
                    if (btnType != null)
                    {
                        btnType.Tag = item.id.ToString();
                        btnType.Text = item.name;
                        btnType.Enabled = true;
                    }
                }
                catch { }
                btnIndex += 1;
            }
            SetParentPageState();
        }

        private void BindChildType(long parentId)
        {
            ClearChildTypeBtn();
            int btnIndex = 1;
            
            if (mChildPageIndex < 1) mChildPageIndex = 1;
            int foodTypeBindType = AppConstVars.systemsetup.foodTypeBindType;
            var childTypeList = new corecmsfoodcategoryServices().QueryChildAsync(ref mChildRowCount, foodTypeBindType, parentId, mChildPageIndex, mChildPageSize);
            foreach (var item in childTypeList)
            {
                string btnName = GetFoodTypeButtonName(flyChildType, btnIndex);
                try
                {
                    UIButton btnType = (UIButton)GetControlInstance(this, btnName);
                    if (btnType != null)
                    {
                        btnType.Tag = item.id.ToString();
                        btnType.Text = item.name;
                        btnType.Enabled = true;
                    }
                }
                catch { }
                btnIndex += 1;
            }

            SetChildPageState();
        }


        private void btnAllType_Click(object sender, EventArgs e)
        {
            mSelectTypeId = 0;
            mParentTypeId = 0;
            mChildPageIndex = 1;
            mFoodPageIndex = 1;
            SetBindFoodList?.Invoke(mSelectTypeId, e);
            BindParentType();
            BindChildType(mParentTypeId);
        }

        private void btnParentType_Click(object sender, EventArgs e)
        {
            var btn = (UIButton)sender;
            btn.Selected = true;
            mParentTypeId = CommFunc.ConvertToInt64(btn.Tag);
            mChildPageIndex = 1;
            mFoodPageIndex = 1;
            BindChildType(mParentTypeId);
            mSelectTypeId = CommFunc.ConvertToInt64(btn.Tag);
            SetBindFoodList?.Invoke(mSelectTypeId, e);
        }

        private void btnChildType_Click(object sender, EventArgs e)
        {
            var btn = (UIButton)sender;
            btn.Selected = true;
            mSelectTypeId = CommFunc.ConvertToInt64(btn.Tag);
            mFoodPageIndex = 1;
            SetBindFoodList?.Invoke(mSelectTypeId, e);
        }


        private void btnParentTypePre_Click(object sender, EventArgs e)
        {
            mParentPageIndex -= 1;
            BindParentType();
        }

        private void btnParentTypeNext_Click(object sender, EventArgs e)
        {
            mParentPageIndex += 1;
            BindParentType();
        }

        private void btnChildTypePre_Click(object sender, EventArgs e)
        {
            mChildPageIndex -= 1;
            BindChildType(mParentTypeId);            
        }
        private void btnChildTypeNext_Click(object sender, EventArgs e)
        {
            mChildPageIndex += 1;
            BindChildType(mParentTypeId);            
        }

        private void btnFoodPre_Click(object sender, EventArgs e)
        {
            mFoodPageIndex -= 1;
            SetBindFoodList?.Invoke(mSelectTypeId, e);
        }

        private void btnFoodNext_Click(object sender, EventArgs e)
        {
            mFoodPageIndex += 1;
            SetBindFoodList?.Invoke(mSelectTypeId, e);
        }
    }
}
